import { Body, Controller, Delete, Get, Inject, Param, Post, Put, UploadedFile } from '@nestjs/common';
import { CreateCategoryDto, UpdateCategoryDto } from './dto/category.dto';
import { CategoryService } from './shop-category.service';
import { RequirePermission } from 'src/common/decorators/require-premission.decorator';
import { UploadService } from 'src/module/upload/upload.service';
import { ResultData } from 'src/common/utils/result';

@Controller('shop/category')
export class CategoryController {
  constructor(
    private readonly categoryService: CategoryService,
    private readonly uploadService: UploadService,
  ) {}

  @RequirePermission('shop:category:add')
  @Post()
  create(@Body() data: CreateCategoryDto) {
    return this.categoryService.create(data);
  }

  @RequirePermission('shop:category:list')
  @Get()
  list() {
    return this.categoryService.findTrees();
  }

  @Get(':id')
  findOne(@Param('id') id: number) {
    return this.categoryService.findOne(id);
  }

  @Put()
  update(@Body() data: UpdateCategoryDto) {
    return this.categoryService.updateCategory(data);
  }

  @Delete(':id')
  delete(@Param('id') id: number) {
    return this.categoryService.remove(id);
  }

  @Post('/upload')
  async categoryImg(@UploadedFile() categoryImg: Express.Multer.File) {
    const res = await this.uploadService.singleFileUpload(categoryImg);
    return ResultData.ok({ imgUrl: res.fileName });
  }
}
